package com.atguigu.crowd.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atguigu.crowd.entity.Auth;
import com.atguigu.crowd.entity.AuthExample;
import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.entity.RoleExample;
import com.atguigu.crowd.entity.RoleExample.Criteria;
import com.atguigu.crowd.mapper.RoleMapper;
import com.atguigu.crowd.service.api.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	RoleMapper roleMapper;
	
	@Override
	public PageInfo<Role> getPageInfo(Integer pageNum, Integer pageSize, String keyword) {
		
		//1、开启分页功能（感叹这个github.pagehelper开发者的非嵌入式设计，太巧秒了）
		PageHelper.startPage(pageNum, pageSize);
		
		//2、执行查询
		List<Role>roleList=roleMapper.selectRoleByKeyword(keyword);
		
		PageInfo<Role> pageinfoRole=new PageInfo<Role>(roleList);
		
		return pageinfoRole;
	}

	@Override
	public void saveRole(Role role) {
		
		roleMapper.insert(role);
		
	}

	@Override
	public int removeRole(List<Integer> roleIds) {
		
		//方式1：
		//试试这样循环删
		for (Integer id : roleIds) {
			roleMapper.deleteByPrimaryKey(id);
		}
		
		//方式2： 使用逆向工程自带的 RoleEcamp 对象
		RoleExample example=new RoleExample();
		
		Criteria criteria =example.createCriteria();
		
		criteria.andIdIn(roleIds);
		
		return roleMapper.deleteByExample(example);
		
	}

	@Override
	public List<Role> getAssignedRole(Integer adminId) {
		return roleMapper.selectAssignedRole(adminId);
	}

	@Override
	public List<Role> getUnAssignedRole(Integer adminId) {
		return roleMapper.selectUnAssignedRole(adminId);
	}

	@Override
	public List<Role> getAll() {
		
		return roleMapper.selectByExample( new RoleExample());
	}
}












